<?php
	function get_events($day,$month,$year) {
		$day = make_two($day);
		$month = make_two($month);
		$date = "$year-$month-$day";
	$sql = "SELECT * FROM `events` WHERE (`approved` = '1' AND `startdate` <= '$date' AND `enddate` > '$date') OR (`approved` = '1' AND `startdate` <= '$date' AND `enddate` = '$date' AND `endtime` != '00:00:00') ORDER BY `starttime` ASC";
		return mysql_array(mysql_query($sql));
		
	}
	
	function get_all_events($upcomming=0) {
		$date = date('Y-m-d');
		$sql = "SELECT * FROM `events` WHERE `approved` = '1'";
		if ($upcomming==1) $sql .= " AND `startdate` >= ' $date'";
		$sql .= " ORDER BY `startdate` ASC";
		return mysql_array(mysql_query($sql));
	}
	
	function get_allday_events($day,$month,$year) {
		$events = get_events($day,$month,$year);
		foreach ($events as $key=>$event) {
			if (should_display_time($event,$year,$month,$day)) unset($events[$key]);
		}
		return $events;
	}
	
	function get_regular_events($day,$month,$year) {
		$events = get_events($day,$month,$year);
		foreach ($events as $key=>$event) {
			if (!should_display_time($event,$year,$month,$day)) unset($events[$key]);
		}
		return $events;
	}
	
	function get_pending_events() {
		return mysql_array(mysql_select("events",array("approved"=>0)));
	}
	
	function approve_event($id) {
		submit_to_gwired($id);
		return mysql_update("events",array("approved"=>1),array("id"=>$id));
	}
	
	function notify_pending() {
		$events = get_pending_events();
		$feeds = get_pending_feeds();
		if (sizeof($events) == 0 && sizeof($feeds) == 0) return false;
		$to = "colonial.calendar@gwstudentassociation.com";
		$headers = 'MIME-Version: 1.0' . "\r\n";
		$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
		$headers .= "To: Colonial.Calendar <$to>" . "\r\n";
		if (sizeof($events) > 0 && sizeof($feeds) == 0) $subject = sizeof($events) . " Pending Events Awaiting Moderation";
		else if (sizeof($feeds) > 0 && sizeof($events) == 0) $subject = sizeof($feeds) . " Pending Calendars Awaiting Moderation";
		else if (sizeof($events) > 0 && sizeof($feeds) > 0) $subject = sizeof($events) . " and " . sizeof($feeds) . " Pending Events Awaiting Moderation";
		$message = "<html>\n<body>\n";
		if (sizeof($events) > 0 ) {
			$message .= "<p><i>The Following events are Pending Moderation:</i></p>\r\n";
			$message .= "<ul>\r\n";
			foreach ($events as $event) {
				$date = explode_date($event['sdate']);
				$user = get_user(get_user_by_event($event['id']));
				$message .= "<li>";
				$message .= "<a href='http://www.gwstudentassociation.com/";
				$message .= $date['year'] . "/" . $date['month'] . "/" . $date['day'] . "/";
				$message .= $event['slug'] .  "/'>";
				$message .= $event['title'];
				$message .= "</a>";
				$message .= " added by " . $user['email'];
				$message .= " [ <a href='http://www.gwstudentassociation.com/calendar/pending.php?action=approve_event&id=" . $event['id'] . "'>Approve</a> ]";
				$message .= "[ <a href='http://www.gwstudentassociation.com/calendar/pending.php?action=remove_event&id=" . $event['id'] . "'>Remove</a> ]";
				$message .= "[ <a href='http://www.gwstudentassociation.com/calendar/pending.php?action=approve_user&id=" . $user['id'] . "'>Approve User</a> ]";
				$message .= "\r\n";
				$message .= "</li>\r\n";
			}
			$message .= "</ul>\r\n";
		}
		if (sizeof($feeds) > 0) {
			$message .= "<p><i>The Following Calendars are Pending Moderation:</i></p>\r\n";
			$message .= "<ul>\r\n";
			foreach ($feeds as $feed) {
				$message .= "<li>" . $feed['name'];
				$message .= " [ <a href='http://www.gwstudentassociation.com/calendar/pending.php?action=approve_feed&id=" . $feed['id'] . "'>Approve</a> ]";
				$message .= "[ <a href='http://www.gwstudentassociation.com/calendar/pending.php?action=remove_feedt&id=" . $feed['id'] . "'>Remove</a> ]";
				$message .= "</li>\r\n";
			}
			$message .= "</ul>\r\n";
		}
		$message .= "</body>\n</html>";
		mail($to, $subject, $message, $headers);
	}
	
	function get_event($day,$month,$year,$slug) {
		$date = "$year-$month-$day";
		return mysql_row_array(mysql_select("events",array("startdate"=>$date,"slug"=>$slug)));
	}
	
	function get_event_by_id($id) {
		return mysql_row_array(mysql_select("events",array("id"=>$id)));
	}
	
	function add_event($title,$user,$allday,$startdate,$starttime,$enddate,$endtime,$desc,$apv) {
		$data = array(
					"title"=>$title,
					"slug"=>make_slug($title),
					"allday"=>$allday,
					"startdate"=>$startdate,
					"starttime"=>$starttime,
					"enddate"=>$enddate,
					"endtime"=>$endtime,
					"desc"=>$desc,
					"approved"=>$apv
		);
		$id=mysql_insert("events",$data);
		user_to_event($id,$user);
		if (!$apv) {
			notify_pending();
		} else if ($user != null) {
			submit_to_gwired($id);
		}
		return $id;
	}
	
		function edit_event($id,$title,$user,$feed,$allday,$startdate,$starttime,$enddate,$endtime,$desc,$apv=0) {
		$data = array(
					"title"=>$title,
					"slug"=>make_slug($title),
					"allday"=>$allday,
					"startdate"=>$startdate,
					"starttime"=>$starttime,
					"enddate"=>$enddate,
					"endtime"=>$endtime,
					"desc"=>$desc,
					"approved"=>$apv
		);
		user_to_event($id,$user);
		$result = mysql_update("events",$data,array("id"=>$id));
		if (!$apv) notify_pending();
		return $result;
	}
	
	function remove_event($id) {
		mysql_remove("events_meta",array("event"=>$id));
		mysql_remove("events_feeds",array("event"=>$id));
		return mysql_remove("events",array("id"=>$id));
	}
	
	function user_to_event($event,$user) {
		if (!$user) return false;
		mysql_remove("events_users",array("event"=>$event));
		return	mysql_insert("events_users",array("event"=>$event, "user"=>$user));
	}
	
	function make_slug($title) {
		$slug = preg_replace("/[^a-zA-Z0-9 ]/", "", $title);
		$slug = str_replace(" ", "-", $slug);
		return strtolower($slug);
	}
	
	function explode_date($date) {
		$date = explode("-",$date);
		return array (	"month" => $date[1],
						"day" => $date[2],
						"year" => $date[0]);
	}
	
	function explode_time_24($time) {
		$time = explode(":",$time);
		return array ( 	"hour"=>$time[0],
						"min"=>$time[1],
						"sec"=>"0");
	}
	
	function explode_time($time) {
		$time = explode(":",$time);
		$hour = $time[0];
		if ($hour >= 12) $suffix = 1;
		else $suffix = 0;  
		if ($hour > 12) $hour = $hour-12;
		return array (	"ampm" => $suffix,
						"min" => $time[1],
						"hour" => $hour);
	}
	
	function format_time($time) {
		$time = explode(":",$time);
		$hour = $time[0];
		$minute = "";
		if ($time[1] != "00") $minute = ":" . $time[1];
		if ($hour >= 12) $suffix = "p";
		else $suffix = "a";
		if ($hour > 12) $hour = $hour-12;
		if ($hour < 10 && strlen($hour) != 1) $hour = substr($hour,1,1);
		return $hour . $minute . $suffix;
	}
	
	function attribute($id) {
		if (mysql_exists("events_users",array("event"=>$id))) {
			$rel = mysql_row_array(mysql_select("events_users",array("event"=>$id)));
			$user = get_user($rel['user']);
			return "Added by <a href='mailto:" . $user['email'] . "@gwmail.gwu.edu'>" . $user['email'] . "</a>";
		} else {
			$rel = mysql_row_array(mysql_select("events_feeds",array("event"=>$id)));
			$feed = get_feed($rel['feed']);
			return "Pulled from " . $feed['name'];
		}
	}
	
function submit_to_gwired($id) {
	
	$event = get_event_by_id($id);
	$user = get_user(get_user_by_event($event['id']));
	$sdate = explode_date($event['startdate']);
	$stime = explode_time($event['starttime']);
	$edate = explode_date($event['enddate']);
	$etime = explode_time($event['endtime']);
	$url = "http://www.gwstudentassociation.com/calendar/" . $sdate['year'] . "/" . $sdate['month'] . "/" . $sdate['day'] . "/" . $event['slug'] . "/";
	if ($stime['ampm'] == 1) $stime['ampm'] = "pm";
	else $stime['ampm'] = "am";
	if ($etime['ampm'] == 1) $etime['ampm'] = "pm";
	else $etime['ampm'] = "am";
	$fields = array(
			"e_ea" => "e",
			"e_title" => $event['title'],
			"e_sponsor" => "via Colonial Calendar",
			"e_description" => $event['desc'],
			"start_month" => $sdate['month'],
			"start_day" => $sdate['day'],
			"start_year" => $sdate['year'],
			"start_hour" => $stime['hour'],
			"start_minute" => $stime['min'],
			"start_ampm" => $stime['ampm'],
			"e_start_date" => "",
			"e_end_date" => "",
			"end_month" => $edate['month'],
			"end_day" => $edate['day'],
			"end_year" => $edate['year'],
			"end_hour" => $etime['hour'],
			"end_minute" => $etime['min'],
			"end_ampm" => $etime['ampm'],
			"rain_month" => 0,
			"rain_day" => 0,
			"rain_year" => 2009,
			"rain_hour" => 1,
			"rain_minute" => 00,
			"rain_ampm" => "am",
			"e_contact_email" => $user['email'] . "@gwmail.gwu.edu",
			"e_url" => $url,
			"type_main" => "2",
			"occasions_list" => "",
			"add_ann" => "yes",
			"Submit" => "Submit your event"
		);
	$url = "http://gwired.gwu.edu/submit4.php";

	foreach($fields as $key=>$value) { 
		$fields_string .= $key.'='.urlencode($value).'&'; 
	}
	rtrim($fields_string,'&');
	$ch = curl_init();
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_POST,count($fields));
	curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
	ob_start();
	$result = curl_exec($ch);
	curl_close($ch);
	ob_end_clean();
}
?>